package com.action;


import java.io.IOException;
import java.io.PrintWriter;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.bean.*;
import com.service.*;
public class useraction extends ActionSupport{
	private User user;
	private userService<User> us;
	
	public String AddUser(){
		try {
			
				us.AddUser(user);
				PrintWriter out = ServletActionContext.getResponse().getWriter();
				out.print("<script>alert('Add User Successfully')</script>");
				out.print("<script>history.go(-1)</script>");
				out.flush();
				out.close();
				return null;
			}
				
		catch (Exception e) {
			// TODO Auto-generated catch block			
			try {
				PrintWriter out = ServletActionContext.getResponse().getWriter();
				out.print("<script>alert('The User's PhoneNumber has existed already')</script>");
				out.print("<script>history.go(-1)</script>");
				out.flush();
				out.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			e.printStackTrace();
		}
		return null;
	}
	
	public String SelectUser(){
		try {
			user = us.SelectUser(user.getPnumber());
			ServletActionContext.getRequest().getSession().setAttribute("userinfo",user);
			ServletActionContext.getRequest().getSession().setMaxInactiveInterval(60*60*12);
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			try {
				
				PrintWriter out = ServletActionContext.getResponse().getWriter();
				out.print("<script>alert('This user does not exist')</script>");
				out.print("<script>history.go(-1)</script>");
				out.flush();
				out.close();
				
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			return null;
			}
			
	}
	
	public String UpdateUser(){
		try {
			us.UpdateUser(user);
			
			PrintWriter out = ServletActionContext.getResponse().getWriter();
			out.print("<script>alert('Modify Successfully')</script>");
			out.print("<script>history.go(-1)</script>");
			out.flush();
			out.close();
			return null;
	
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public userService<User> getUs() {
		return us;
	}

	public void setUs(userService<User> us) {
		this.us = us;
	}
	

}